Repository navigation
Worker-safety check: see a scriptblock through parentheses, casts and GetNewClosure - #97
Merged
Merged
Conversation
Two of the batch's own closures called Complete-PkProcess and Write-PkMessage by name. A .GetNewClosure() block only sees global functions, so a batch failed with "'Complete-PkProcess' is not recognized" when the kit was loaded for one module only. They're captured up front now, like the private helpers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A worker given the function (-SharedFunctionNames takes it) could set a handler made in its own runspace, which the main thread then ran for every message. Running another runspace's scriptblock can hang or crash PowerShell. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It only looked for an abort after its first 200 ms wait, so a program that finished sooner ran to the end and returned normally, and a slower one ran for about 200 ms before it was killed. It now throws before starting the program, unless -IgnoreAbort is given. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… GetNewClosure
DelegateCast missed a scriptblock passed to .NET as ({ 1 }), { 1 }.GetNewClosure(),
[scriptblock]{ 1 } and the like, in the call or through a variable.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The worker-safety check's
DelegateCastrule flags a scriptblock passed to a .NET method or constructor, which .NET turns into a delegate that can hang or crash a worker. It missed one with anything around it:({ 1 }),{ 1 }.GetNewClosure(),({ 1 }).GetNewClosure(),[scriptblock]{ 1 }or$sb.GetNewClosure()in the call, and$sb = ({ 1 }),$sb = ({ 1 }).GetNewClosure()or$sb = [scriptblock]{ 1 }passed later.{ 1 }.GetNewClosure()in the call is the likeliest one in real code.Get-PkUnwrappedExpression, takes off parentheses,[scriptblock]casts and.GetNewClosure()calls, in any order. It's used on each argument and on an assignment's right side, which had its own partial version of this.Invoke-PkRunspaceJobsrefuses a worker this newly flags.# pk:ignoreon the line still accepts one whose method really takes a scriptblock.Tests
Section P tries eight wrapped forms, each passed directly and through a variable, and four ways to wrap the variable itself. It also checks that other parenthesized or cast values,
.ForEach(($sb)),.Add(($sb))and a# pk:ignoreline aren't flagged.a wrapped scriptblock passed to .NET is flagged, written in the call or through a variable (missed: passed ({ 1 }) | ...).{ 1 }.GetNewClosure()assigned to a variable was the only form seen.Testing/readme-examples/run.ps1passes on both, so no README worker is newly flagged.🤖 Generated with Claude Code